package files;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.io.Files;

public class FileLoader {
	private static final Logger log = LoggerFactory.getLogger(FileLoader.class);
	private static String DEFAULT_EXTENSION = "txt";

	public static List<File> getInfoFiles(String pathname) {
		File dir = new File(pathname);

		if (!dir.isDirectory())
			throw new IllegalArgumentException(
					"Selected path is not a directory.");

		Optional<File[]> opt = Optional.of(dir.listFiles());
		List<File> infoFiles = new ArrayList<File>();

		if (opt.isPresent()) {
			for (File innerDir : opt.get()) {
				if (Optional.of(innerDir.listFiles()).isPresent())
					for (File file : innerDir.listFiles()) {
						Optional<String> optExt = Optional.of(Files
								.getFileExtension(file.getName()));

						if (optExt.isPresent()
								&& DEFAULT_EXTENSION.equals(optExt.get().trim()
										.toLowerCase())) {
							infoFiles.add(file);

							if (log.isDebugEnabled()) {
								log.debug(
										"Found file: '{}' in directory: '{}'",
										file.getName(), file.getAbsolutePath());
							}
						}
					}
			}
		}

		return infoFiles;
	}
}
